import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import { jsx as _jsx } from "react/jsx-runtime";
import { Input } from 'antd-v4';
import React from 'react';

var languageFormat = function languageFormat(text, language) {
  if (typeof text !== 'string') {
    return text;
  }

  try {
    if (language === 'json') {
      return JSON.stringify(JSON.parse(text), null, 2);
    }
  } catch (error) {// console.log(error)
  }

  return text;
};
/**
 * 代码片段组件 这个组件为了显示简单的配置，复杂的请使用更加重型的组件
 *
 * @param
 */


var FieldCode = function FieldCode(_ref, ref) {
  var text = _ref.text,
      mode = _ref.mode,
      render = _ref.render,
      _ref$language = _ref.language,
      language = _ref$language === void 0 ? 'text' : _ref$language,
      renderFormItem = _ref.renderFormItem,
      plain = _ref.plain,
      fieldProps = _ref.fieldProps;
  var code = languageFormat(text, language);

  if (mode === 'read') {
    var dom = _jsx("pre", _objectSpread(_objectSpread({
      ref: ref
    }, fieldProps), {}, {
      style: _objectSpread({
        padding: 16,
        overflow: 'auto',
        fontSize: '85%',
        lineHeight: 1.45,
        backgroundColor: '#f6f8fa',
        borderRadius: 3,
        width: 'min-content'
      }, fieldProps.style),
      children: _jsx("code", {
        children: code
      })
    }));

    if (render) {
      return render(code, _objectSpread(_objectSpread({
        mode: mode
      }, fieldProps), {}, {
        ref: ref
      }), dom);
    }

    return dom;
  }

  if (mode === 'edit' || mode === 'update') {
    var _dom = _jsx(Input.TextArea, _objectSpread(_objectSpread({
      rows: 5
    }, fieldProps), {}, {
      ref: ref
    }));

    if (plain) {
      _dom = _jsx(Input, _objectSpread(_objectSpread({}, fieldProps), {}, {
        ref: ref
      }));
    }

    if (renderFormItem) {
      return renderFormItem(code, _objectSpread(_objectSpread({
        mode: mode
      }, fieldProps), {}, {
        ref: ref
      }), _dom);
    }

    return _dom;
  }

  return null;
};

export default /*#__PURE__*/React.forwardRef(FieldCode);